from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver import EdgeOptions, ChromeOptions

from tools.reader.config_reader import ConfigReader


def _create_service():
    """Create and return the service."""
    # 获取配置文件中浏览器驱动位置
    directory_path = ConfigReader().read("driver")["edge_driver_path"]
    # 获取本机浏览器驱动版本
    edge_version = UpdateEdge().get_version()
    driver_path = f"{directory_path}/{edge_version}"
    executable_path = f"{driver_path}/msedgedriver.exe"
    service = Service(executable_path=executable_path)
    return service


def _configure_edge_options():
    """Configure Edge options."""
    edge_options = EdgeOptions()
    edge_options.use_chromium = True
    edge_config = ConfigReader().read("options")

    window_size = edge_config["window_size"].lower()  # 窗口尺寸
    headless = edge_config["headless"].lower()  # 无头模式
    disable_gpu = edge_config["disable_gpu"].lower()  # 禁用GPU加速
    disable_gpu_program_cache = edge_config["gpu_program_cache"].lower()  # 禁用GPU程序缓存
    disable_javascript = edge_config["javascript"].lower()  # 禁用运行JavaScript代码
    enable_automation = edge_config["automation"].lower()  # 允许通过自动测试控制浏览器

    if window_size == "max" or window_size == "MAX":
        edge_options.add_argument("--start-maximized")
    elif window_size == "cus" or window_size == "CUS":
        wide = edge_config["wide"]
        high = edge_config["high"]
        edge_options.add_argument("--window-size=" + wide + "," + high)
    if headless == "true" or headless == "TRUE":
        edge_options.add_argument("--headless")
    elif headless == "incognito":
        edge_options.add_argument("--incognito")
    if disable_gpu == "true" or disable_gpu == "TRUE":
        edge_options.add_argument("--disable-gpu")
    if disable_gpu_program_cache == "true" or disable_gpu_program_cache == "TRUE":
        edge_options.add_argument("--disable-gpu-program-cache")
    if disable_javascript == "true" or disable_javascript == "TRUE":
        edge_options.add_argument("--disable-javascript")
    if enable_automation == "true" or enable_automation == "TRUE":
        edge_options.add_argument("--enable-automation")
    return edge_options


def _configure_chrome_options():
    """Configure Chrome options."""
    chrome_options = ChromeOptions()
    chrome_config = ConfigReader().read("options")

    window_size = chrome_config["window_size"].lower()  # 窗口尺寸
    headless = chrome_config["headless"].lower()  # 无头模式
    disable_gpu = chrome_config["disable_gpu"].lower()  # 禁用GPU加速
    disable_gpu_program_cache = chrome_config["gpu_program_cache"].lower()  # 禁用GPU程序缓存
    disable_javascript = chrome_config["javascript"].lower()  # 禁用运行JavaScript代码
    enable_automation = chrome_config["automation"].lower()  # 允许通过自动测试控制浏览器

    if window_size == "max" or window_size == "MAX":
        chrome_options.add_argument("--start-maximized")
    elif window_size == "cus" or window_size == "CUS":
        wide = chrome_config["wide"]
        high = chrome_config["high"]
        chrome_options.add_argument("--window-size=" + wide + "," + high)
    if headless == "true" or headless == "TRUE":
        chrome_options.add_argument("--headless")
    elif headless == "incognito":
        chrome_options.add_argument("--incognito")
    if disable_gpu == "true" or disable_gpu == "TRUE":
        chrome_options.add_argument("--disable-gpu")
    if disable_gpu_program_cache == "true" or disable_gpu_program_cache == "TRUE":
        chrome_options.add_argument("--disable-gpu-program-cache")
    if disable_javascript == "true" or disable_javascript == "TRUE":
        chrome_options.add_argument("--disable-javascript")
    if enable_automation == "true" or enable_automation == "TRUE":
        chrome_options.add_argument("--enable-automation")
    return chrome_options


class Assembler:

    def __init__(self):
        self.driver = None
        self.assemble_driver()

    # 驱动装配器
    def assemble_driver(self):
        # 获取配置文件中的driver类型
        driver_type = ConfigReader().read("project")["driver"].lower()
        # 目前只配置了edge浏览器的驱动
        if driver_type == "edge" or driver_type == "EDGE":
            # 配置options
            edge_options = _configure_edge_options()
            # 配置service
            service = _create_service()
            self.driver = webdriver.Edge(options=edge_options, service=service)

        else:
            self.driver = None
            raise TypeError("Unsupported driver type")

    def disassemble_driver(self):
        """
        Uninstall the driver
        """
        if self.driver is not None:
            self.driver.quit()
            self.driver = None

    def get_driver(self):
        """
        Get the driver
        """
        return self.driver
